package com.tencent.mlvb.apilive.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.flyco.tablayout.listener.OnTabSelectListener;
import com.tencent.mlvb.apilive.ExitApplication;
import com.tencent.mlvb.apilive.bean.JobRegion;
import com.tencent.mlvb.apilive.bean.Manipulators;
import com.tencent.mlvb.apilive.databinding.ActivityMainBinding;
import com.tencent.mlvb.apilive.network.APIHost;
import com.tencent.mlvb.apilive.network.ApiRequestService;
import com.tencent.mlvb.apilive.network.CallBackLis;
import com.tencent.mlvb.apilive.network.HttpCall;
import com.tencent.mlvb.apilive.network.ResponseModel;
import com.tencent.mlvb.apilive.network.RetrofitHelper;
import com.tencent.mlvb.apilive.utils.Md5Util;
import com.tencent.mlvb.apilive.utils.SpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.Observable;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private  String[] mTitles_1;
    private  String[] mTitles_2;
    private List<JobRegion> jobRegionList = new ArrayList<>();
    private List<Manipulators> manipulatorsList = new ArrayList<>();
    private JobRegion defaultJobRegion ;
    private Manipulators defaultManipulators ;
    private boolean isLive;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ExitApplication.getInstance().addActivity(this);
        SpUtils.putBoolean(getApplicationContext(), "isLive", false);
        initView();
        initData();

    }

    private void initView(){
            binding.tabLayout1.setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelect(int position) {
                    defaultJobRegion = jobRegionList.get(position);
                    Toast.makeText(MainActivity.this,defaultJobRegion.getLabel(),Toast.LENGTH_LONG).show();
                }

                @Override
                public void onTabReselect(int position) {

                }
            });
            binding.tabLayout2.setOnTabSelectListener(new OnTabSelectListener() {
                @Override
                public void onTabSelect(int position) {
                    defaultManipulators = manipulatorsList.get(position);
                    Toast.makeText(MainActivity.this,defaultManipulators.getLabel(),Toast.LENGTH_LONG).show();
                }

                @Override
                public void onTabReselect(int position) {

                }
            });
        binding.tvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLive){
                    Toast.makeText(MainActivity.this, "当前正在直播,请先结束直播", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(MainActivity.this, LiveInfoActivity.class);
                intent.putExtra("code",defaultJobRegion.getStrValue());
                intent.putExtra("uavId",defaultManipulators.getIntValue());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
       boolean isLive = SpUtils.getBoolean(getApplicationContext(), "isLive", false);
        if (isLive){
            moveTaskToBack(true);
        }
    }

    private void initData(){
        login();
    }
    private void login(){
        Map<String,String> hashMap = new HashMap();
        hashMap.put("username", "5cyd2m");
        String mapToMD5String = Md5Util.getMapToMD5String(hashMap);

//        RequestBody requestBody = ApiRequestBody.shareInstance().createRequestBody(JSON.toJSONString(hashMap, SerializerFeature.WriteMapNullValue));

        Observable<ResponseModel<String>> observable = RetrofitHelper.getInstance().getRetrofit().create(ApiRequestService.class).origin("5cyd2m",mapToMD5String);
        HttpCall.doCall(this, observable, new CallBackLis<String>(){
            @Override
            public void onSuccess(String method, String content) {
                APIHost.token = content;
                jobRegion();
                getManipulators();
            }

            @Override
            public void onFailure(String method, String error) {
                Log.e("请求错误:", error);
            }
        }, null);
    }
    private void jobRegion(){
        Observable<ResponseModel<List<JobRegion>>> observable = RetrofitHelper.getInstance().getRetrofit().create(ApiRequestService.class).jobRegion();
        HttpCall.doCall(this, observable, new CallBackLis<List<JobRegion>>(){
            @Override
            public void onSuccess(String method, List<JobRegion> list) {
                setobRegionUI(list);
            }

            @Override
            public void onFailure(String method, String error) {

            }
        }, null);
    }
    private void getManipulators(){
        Observable<ResponseModel<List<Manipulators>>> observable = RetrofitHelper.getInstance().getRetrofit().create(ApiRequestService.class).getManipulators();
        HttpCall.doCall(this, observable, new CallBackLis<List<Manipulators>>(){
            @Override
            public void onSuccess(String method, List<Manipulators> manipulatorsList) {
                setManipulatorUI(manipulatorsList);
            }

            @Override
            public void onFailure(String method, String error) {

            }
        }, null);
    }

    private void setobRegionUI(List<JobRegion> list){
        jobRegionList = list;
        defaultJobRegion = list.get(0);
        List<String> strings = new ArrayList<>();
        for (JobRegion jobRegion : list) {
            strings.add(jobRegion.getLabel());
        }
        mTitles_1 = strings.toArray(new String[strings.size()]);
        binding.tabLayout1.setTabData(mTitles_1);
    }
    private void setManipulatorUI(List<Manipulators> list){
        manipulatorsList = list;
        defaultManipulators = list.get(0);
        List<String> strings = new ArrayList<>();
        for (Manipulators manipulators : list) {
            strings.add(manipulators.getLabel());
        }
        mTitles_2 = strings.toArray(new String[strings.size()]);
        binding.tabLayout2.setTabData(mTitles_2);
    }
}

